﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace RenameTools
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            chkClearWord.CheckedChanged += new EventHandler(Preview);
            txtClearWord.TextChanged += new EventHandler(Preview);
            chkNumAddDec.CheckedChanged += new EventHandler(Preview);
            txtNumAddDec.TextChanged += new EventHandler(Preview);
            chkNumLength.CheckedChanged += new EventHandler(Preview);
            txtNumLength.TextChanged += new EventHandler(Preview);

            dlg = new OpenFileDialog();
            dlg.Multiselect = true;
        }

        OpenFileDialog dlg;

        private void btnSelectFiles_Click(object sender, EventArgs e)
        {
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Preview(sender, e);
            }
        }

        public string ClearWord
        {
            get
            {
                if (chkClearWord.Checked && txtClearWord.Text != string.Empty)
                {
                    return txtClearWord.Text;
                }
                return null;
            }
        }

        public int NumAddDec
        {
            get
            {
                if (chkNumAddDec.Checked)
                {
                    int i;
                    if (int.TryParse(txtNumAddDec.Text.Trim(), out i))
                    {
                        return i;
                    }
                }
                return 0;
            }
        }

        public int NumLength
        {
            get
            {
                if (chkNumLength.Checked)
                {
                    int i;
                    if (int.TryParse(txtNumLength.Text.Trim(), out i))
                    {
                        return i;
                    }
                }

                return 0;
            }
        }

        void Preview(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            //lstFiles.Items.Clear();
            if (dlg.FileNames != null && dlg.FileNames.Length > 0)
            {
                foreach (string item in dlg.FileNames)
                {
                    RenameFile file = new RenameFile(item);
                    string newFileName = file.FileName;

                    if (ClearWord != null)
                    {
                        while (newFileName.IndexOf(ClearWord) >= 0)
                        {
                            newFileName = newFileName.Remove(newFileName.IndexOf(ClearWord), ClearWord.Length);
                        }
                    }

                    if (NumAddDec != 0)
                    {
                        while (newFileName.IndexOf(' ') >= 0)
                        {
                            newFileName = newFileName.Remove(newFileName.IndexOf(' '), 1);
                        }

                        int i;
                        if (int.TryParse(newFileName, out i))
                        {
                            newFileName = (i + NumAddDec).ToString();
                        }
                    }

                    if (NumLength != 0)
                    {
                        while (newFileName.Trim().Length < NumLength)
                        {
                            newFileName = "0" + newFileName;
                        }
                    }

                    file.NewFileName = newFileName;
                    ListViewItem lvi = new ListViewItem(file.FileName);
                    lvi.SubItems.Add(file.NewFileName);
                    lvi.Tag = file;
                    listView1.Items.Add(lvi);
                    //listView1.Items.Add(new ListViewItem);
                    //lstFiles.Items.Add(file);
                }
                btnStartRename.Enabled = true;
            }
        }

        private void btnStartRename_Click(object sender, EventArgs e)
        {
            int count = listView1.Items.Count; // lstFiles.Items.Count;
            int finished = 0;
            foreach (ListViewItem item in listView1.Items)
            {
                RenameFile file = item.Tag as RenameFile;
                if (file != null)
                {
                    file.StartRename();
                }
                finished++;
                double d = finished / count * 100;
                pbStartRename.Value = (int)d;
            }

            if (MessageBox.Show("批量重命名成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.OK)
            {
                pbStartRename.Value = 0;
                btnStartRename.Enabled = false;
            }
        }

        private void btnAutoCheck_Click(object sender, EventArgs e)
        {
            if (listView1.Items.Count > 0)
            {
                RenameFile file = listView1.Items[0].Tag as RenameFile;
                string fileName = file.FileName;
                StringBuilder tmp = new StringBuilder();
                foreach (char item in fileName)
                {
                    int i;
                    if (int.TryParse(new string(new char[] { item }), out i) == false)
                    {
                        tmp.Append(item);
                    }
                }
                txtClearWord.Text = tmp.ToString();
            }

        }

        private void lstFiles_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.Items.Count > 0 && listView1.SelectedItems.Count >0)
            {
                RenameFile file = listView1.SelectedItems[0].Tag as RenameFile;
                Process.Start("explorer.exe", file.FilePath.Substring(0, file.FilePath.IndexOf(file.FileName)));
            }
        }

        private void miAbout_Click(object sender, EventArgs e)
        {
            AboutForm frm = new AboutForm();
            frm.ShowDialog();
        }
    }

    public class RenameFile
    {
        public string FilePath { get; set; }

        public string NewFileName { get; set; }

        public RenameFile(string filePath)
        {
            this.FilePath = filePath;
        }

        public string FileName
        {
            get
            {
                if (FilePath != null)
                {
                    return FilePath.Substring(FilePath.LastIndexOf(@"\") + 1, FilePath.LastIndexOf(".") - FilePath.LastIndexOf(@"\") - 1);
                }
                return null;
            }
        }

        public string Ext
        {
            get
            {
                if (FilePath != null)
                {
                    return FilePath.Substring(FilePath.LastIndexOf(@"."));
                }
                return null;
            }
        }

        public void StartRename()
        {
            if (File.Exists(FilePath))
            {
                string desc = FilePath.Substring(0, FilePath.LastIndexOf(@"\")) + @"\" + NewFileName + Ext;
                File.Move(FilePath, desc);
            }
        }

        public override string ToString()
        {
            return FileName + Ext + " -> " + NewFileName + Ext;
        }
    }
}
